/*******************************************************************************
 * Copyright (c) 2013 Michele Nanna.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Michele Nanna - initial API and implementation
 ******************************************************************************/
package com.systemfastforward.provinciawifi.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.cyrilmottier.polaris.Annotation;
import com.google.android.maps.GeoPoint;

/**
 * Utils method for access point list
 * 
 * @author Michele Nanna
 *
 */
public class AccessPointUtils {

	/**
	 * Create a list of Annotation for displayng into a mapView from a list of AccessPoint. Note that access point list can have
	 * multiple occurences of same access point denomination according dataset description, so it also group same access point 
	 * into one Annotation entry.
	 * 
	 * @param accessPointList the list to convert
	 * @return the created list
	 */
	public static List<Annotation> buildAnnotationlistFromAccessPointList(List<AccessPoint> accessPointList) {
		List<Annotation> accessPointAnnotationList = new ArrayList<Annotation>(accessPointList.size());
		
		if(accessPointList == null || (accessPointList.size() == 0)){
			return accessPointAnnotationList;
		}
		
		// ordering of accesspoint list
		Collections.sort(accessPointList);
		
		AccessPoint oldAp = accessPointList.get(0);
		for(AccessPoint ap : accessPointList){
			if(!ap.getDenominazione().equalsIgnoreCase(oldAp.getDenominazione())){
				
				// create and add old access point to list
				Annotation an = new Annotation(
						new GeoPoint((int)(oldAp.getLatitudine() * 1E6), (int)(oldAp.getLongitudine() * 1E6)), 
						oldAp.getDenominazione(),
						oldAp.getIndirizzoCompleto());
				accessPointAnnotationList.add(an);
				
				// update old access point object
				oldAp = ap;
			}
		}
		return accessPointAnnotationList;
	}
	
	/**
	 * Filter the access point list by Comuni list
	 * 
	 * @param comuni
	 */
	public static void filterAccessPointListByComuni(String[] comuni, boolean[] comuniChecked, List<AccessPoint> apCompleteList, List<AccessPoint> apFilteredList) {
		String comune = null;
		apFilteredList.clear();
		for (int i=0; i<comuni.length; i++) {
			comune = comuni[i];
			for (AccessPoint ap : apCompleteList) {
				if(ap.getComune().equalsIgnoreCase(comune) && comuniChecked[i]){
					apFilteredList.add(ap);
				}
			}
		}
	}
	
	public static void filterAccessPointListByComune(String comune, List<AccessPoint> apCompleteList, List<AccessPoint> apFilteredList) {
		apFilteredList.clear();
		for (AccessPoint ap : apCompleteList) {
			if (ap.getComune().equalsIgnoreCase(comune)) {
				apFilteredList.add(ap);
			}
		}
	}
	
	/**
	 * Get list of comuni from The access point list in input
	 * 
	 * @param apList list of access point
	 * @return list of comuni
	 */
	public static List<String> getComuniFromAccessPointList(List<AccessPoint> apList){
		List<String> comuniList = new ArrayList<String>();
		
		// sort by comuni
		Collections.sort(apList, new AccessPoint.ComuneComparator());
		
		String comune = apList.get(0).getComune();
		for(AccessPoint ap : apList){
			if(!ap.getComune().equalsIgnoreCase(comune)){
				comuniList.add(comune);
				comune = ap.getComune();
			}
		}
		comuniList.add(comune);
		
		return comuniList;
	}
}
